<?php
class Form_Base
{
    public function primary_key()
    {
        $key = DB::select()
            ->from('form_config')
            ->where('name', '=', $this->_type)
            ->limit(1)
            ->execute()
            ->get('primary_key');

        return $key;
    }

    public function getFormItemsConfig ($type)
    {
        $query = DB::select()
            ->from('form_items_config')
            ->where('type', '=', $type)
            ->order_by('sort', 'asc')
            ->execute();

        return $query->as_array();
    }


    public function primary_table ()
    {
        $key = DB::select()
            ->from('form_config')
            ->where('name', '=', $this->_type)
            ->limit(1)
            ->execute()
            ->get('table');

        return $key;
    }

    public function order_by ()
    {
        $query = DB::select()
            ->from('form_config')
            ->where('name', '=', $this->_type)
            ->limit(1)
            ->execute()
            ->current();

        return array($query['list_sort_key'], $query['list_sort_type']);
    }

    public function include_edit_file ()
    {
        $query = DB::select()
            ->from('form_config')
            ->where('name', '=', $this->_type)
            ->limit(1)
            ->execute()
            ->current();

        $tpl = $query['include_edit_file'];
        return $tpl;
    }
    
    public function include_head_file ()
    {
    	$query = DB::select()
    	->from('form_config')
    	->where('name', '=', $this->_type)
    	->limit(1)
    	->execute()
    	->current();
    
    	$tpl = $query['include_head_file'];
    	return $tpl;
    }

    protected function build_buttons($type)
    {
        $request = Request::current();
        $cont  = $request->controller();
        $dirt  = $request->directory();
        $query = $_SERVER['QUERY_STRING'];
        $query = empty($query) ? '' : '?'.$query;

        $dirt = empty($dirt) ? '' : '/'.$dirt;

        if (($cont == 'userv2' && $type == 'create')
            || ($cont == 'userv2coin' && $type == 'create')
            )
            return '';

        if ($type == 'create')
            return "<a href=\"$dirt/$cont/create{$query}\">创建</a>";
        else if ($type == 'list')
            return "<a href=\"$dirt/$cont/list{$query}\">列表</a>";
        else 
            return '';
    }

}
